const { query } = require('./config/database')

async function testChapters () {
  try {
    console.log('🔍 测试章节数据...')

    // 1. 查看斗罗大陆小说信息
    const novels = await query('SELECT * FROM novels WHERE title = ?', [
      '斗罗大陆'
    ])
    if (novels.length > 0) {
      const novel = novels[0]
      console.log('📚 斗罗大陆小说信息:')
      console.log(`   - ID: ${novel.id}`)
      console.log(`   - 标题: ${novel.title}`)
      console.log(`   - 作者: ${novel.author}`)
      console.log(`   - 总字数: ${novel.word_count?.toLocaleString()}`)
      console.log(`   - 章节数: ${novel.chapter_count}`)

      // 2. 查看章节列表
      const chapters = await query(
        'SELECT id, title, chapter_number, word_count, is_vip, price FROM chapters WHERE novel_id = ? ORDER BY chapter_number LIMIT 10',
        [novel.id]
      )

      console.log('\n📄 前10章节信息:')
      chapters.forEach((chapter, index) => {
        console.log(
          `   ${index + 1}. [${chapter.id}] 第${chapter.chapter_number}章: ${
            chapter.title
          }`
        )
        console.log(
          `      字数: ${chapter.word_count} | VIP: ${
            chapter.is_vip ? '是' : '否'
          } | 价格: ${chapter.price}`
        )
      })

      // 3. 查看第一章内容（部分）
      if (chapters.length > 0) {
        const firstChapter = await query(
          'SELECT title, content FROM chapters WHERE id = ?',
          [chapters[0].id]
        )

        if (firstChapter.length > 0) {
          const content = firstChapter[0].content
          console.log('\n📖 第一章内容预览:')
          console.log(`   标题: ${firstChapter[0].title}`)
          console.log(`   内容: ${content.substring(0, 200)}...`)
        }
      }
    } else {
      console.log('❌ 未找到斗罗大陆小说')
    }
  } catch (error) {
    console.error('❌ 测试失败:', error)
  }
}

testChapters().then(() => {
  console.log('\n✅ 测试完成')
  process.exit(0)
})
